;
(function() {
	var skill = {
		_getGoodsIdList: function(options) {
			var goods_list = this.goods_list
			if (!(options && options.show_un_sold_goods)) {
				var bus = BusCon.get(this.bus_id);
				goods_list = bus._filter_un_sold_goods(goods_list);
			}
			return goods_list;
		},
		_getGoods: function(goods_id) {
			if (this._getGoodsIdList().indexOf(goods_id) === -1) {
				throwCusError("该商品不属于指定会员专区");
			}
			return GoodsCon.get(goods_id);
		},
		getGoodsList: function(num, page, options) {
			return $Array.clearNull(data_cut.page(this._getGoodsIdList(options), num, page).map(function(goods_id) {
				return GoodsCon.get(goods_id);
			}));
		},
		getGoodsList_PageInfo: function(num, page, options) {
			var result = this.getGoodsList(num, page, options);
			var total_num = this._getGoodsIdList(options).length;
			return {
				total_num: total_num,
				total_page: Math.round(total_num / num) || 1, //至少一页
				goods: result,
				page: +page || 0,
				num: +num || 0
			}
		},
		_addGoods: function(goods_id) {
			var goods = GoodsCon.get(goods_id);
			if (goods.bus_id !== this.bus_id) {
				throwCusError("无权操作此商品");
			}
			this._getGoodsIdList().push(goods_id);
			this.save();
			return goods;
		},
		addGoods: function(goods_id) {
			//添加到商品列表中
			var goods = this._addGoods(goods_id);
			//商品中记录专区
			goods._addToCardFactoryList(this._id);

			//会员卡的优惠是针对这个商家所有的商品
			// //将BUFF添加给商品
			// goods._addBuff(this.buff_id);
			return goods;
		},
		updateGoods: function(goods_id, goods_info) {
			var goods = this._getGoods(goods_id);
			goods._setInfo(goods_info);
			//专区暂时没有特定标签，所以无需处理商品的标签
			return goods;
		},
		updateGoodsIndex: function(goods_id, index) {
			var goods_list = this._getGoodsIdList();
			var old_index = goods_list.indexOf(goods_id);
			if (old_index === -1) {
				throwCusError("商品不属于指定会员卡");
			}
			goods_list.splice(old_index, 1);
			index = ~~index;
			if (index > goods_list.length) {
				goods_list.push(goods_id);
			} else if (index < 0) {
				goods_list.unshift(goods_id);
			} else {
				goods_list.splice(index, 0, goods_id)
			}
			this.goods_list = goods_list;
			this.save();
			return {
				old_index: old_index,
				new_index: index
			};
		},
		_removeGoods: function(goods_id) {
			this.goods_list = $Array.remove(this._getGoodsIdList(), goods_id);
			this.save();
			return this.goods_list
		},
		removeGoods: function(goods_id) {
			var goods = this._getGoods(goods_id);
			//从商品列表中删除
			this._removeGoods(goods_id);
			//商品中删除专区
			return goods._removeFromCardFactoryList(this._id);
		}
	};
	module.exports = skill;

	var db = require("../db/index");
	var CardCon = require("../card/_con");
	var CardFactoryCon = require("../card_factory/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
}());